$(document).ready(function() {
    $('#file_upload').uploadify({
        'swf'       : '/static/uploadify/uploadify.swf',
        'uploader'  : '/uploadFile/',
        'auto'      : true,
        'requeueErrors':false,
        'buttonText':'上传',
        'removeCompleted':true,
        'onInit': function () {
          //console.log(ready);
        },
        'onError' : function (event,ID,fileObj,errorObj) {
            $('#id_span_msg').html("上传失败，错误码:"+errorObj.type+" "+errorObj.info);
        },
        'onSelect': function (e, queueId, fileObj) {
            $('#id_span_msg').html("");
        },
        'onCancel'    : function(event,ID,fileObj,data) {

        },
        'onUploadSuccess': function(file,data,response) {
            //console.log(data);
        }
    });

    $('.deleteFile').click(function() {
      if(confirm('确定删除？')) {
        var file=$(this).parent().attr("id")
        $.ajax({
            url: '/deleteFile/',
            type: 'POST',
            dataType: 'json',
            data: {
                file:file
            },
            success:function(callback){
                //console.log(callback);
            }
        })
        .done(function() {
            //console.log('success');

        })
        .fail(function() {
            //console.log("error");
        })
        .always(function() {
            //console.log("complete");
            window.location.reload();
        });
      }
    });

    $('.downloadFile').click(function() {
        var file=$(this).parent().attr("id");
        var a = $(this).parent();
        $.ajax({
            url: '/checkFile/',
            type: 'GET',
            dataType: 'json',
            data: {
                'file': file,
            },
            success:function(callback){
                if(callback == 'ok') {
                  window.location="/downloadFile/?file="+file;
                  var b = a.parent().find('span').filter('#downloadNum').html();
                  //alert(b);
                  a.parent().find('span').filter('#downloadNum').html(parseInt(b) + 1);
                }
                else {
                  window.location.reload();
                }
            }
        });

    });
});
